/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package admin.BusinessLogic;

import DAO.DAO;
import DTO.Category;
import DTO.Clerk;
import DTO.Item;
import DTO.Order;
import DTO.Promotion;
import DTO.Store;
import Exception.KwickKioskDBException;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
 *
 * @author WEN
 */
public class BusinessLogic {

    private DAO dao;
    private Vector clerkVec;
    private Vector storeVec;
    private Vector promoVec;
    private Vector itemVec;
    private Vector orderVec;
    boolean bool;
    String user [];
    public BusinessLogic() throws FileNotFoundException, IOException {
        bool = false;
        user = new String[3];
       
            FileInputStream fstream = new FileInputStream("config.txt");
            // Get the object of DataInputStream
            DataInputStream in = new DataInputStream(fstream);
            BufferedReader br = new BufferedReader(new InputStreamReader(in));
            String strLine;
            int i = 0;
            while ((strLine = br.readLine()) != null) {
                // Print the content on the console    
                user[i] = strLine;
                i++;
                System.out.println(strLine);
            }
         try {
            dao = new DAO(user[0],user[1],user[2]);
        } catch (KwickKioskDBException ex) {
            Logger.getLogger(BusinessLogic.class.getName()).log(Level.SEVERE,
                    null, ex);
            JOptionPane.showMessageDialog(null, "Sorry! Can not connect to the server",
                    "Error", JOptionPane.ERROR_MESSAGE);
        }
        clerkVec = new Vector();
        storeVec = new Vector();
        promoVec = new Vector();
        itemVec = new Vector();
        orderVec = new Vector();
    }

    public boolean loginLogical(String userID, String pwd) {
        String password = "";
        try {
            if (dao.getAdminInfo(Integer.parseInt(userID)) == null) {
                return false;
            }
            password = dao.getAdminInfo(Integer.parseInt(userID)).getPasscode();
        } catch (KwickKioskDBException ex) {
            JOptionPane.showMessageDialog(null, "Sorry! Can not connect to the server",
                    "Error", JOptionPane.ERROR_MESSAGE);
            Logger.getLogger(BusinessLogic.class.getName()).log(Level.SEVERE,
                    null, ex);
        }
        if (pwd.equals(password)) {//(decryptedPwd)) {
            return true;
        } else {
            return false;
        }
    }

    public Vector getClerk() {
        clerkVec.clear();
        try {
            clerkVec = transformToVector(dao.getAllClerks());
        } catch (KwickKioskDBException ex) {
            JOptionPane.showMessageDialog(null, "Sorry! Can not connect to the server",
                    "Error", JOptionPane.ERROR_MESSAGE);
            Logger.getLogger(BusinessLogic.class.getName()).log(Level.SEVERE, null, ex);

        }
        return clerkVec;
    }

    public Vector getClerksByFirstName(String name) {
        clerkVec.clear();
        try {
            clerkVec = transformToVector(dao.getClerksByFirstName(name));
        } catch (KwickKioskDBException ex) {
            JOptionPane.showMessageDialog(null, "Sorry! Can not connect to the server",
                    "Error", JOptionPane.ERROR_MESSAGE);
            Logger.getLogger(BusinessLogic.class.getName()).log(Level.SEVERE, null, ex);

        }
        return clerkVec;
    }

    public Vector getClerksByLastName(String LastName) {
        clerkVec.clear();
        try {
            clerkVec = transformToVector(dao.getClerksByLastName(LastName));
        } catch (KwickKioskDBException ex) {

            JOptionPane.showMessageDialog(null, "Sorry! Can not connect to the server",
                    "Error", JOptionPane.ERROR_MESSAGE);
            Logger.getLogger(BusinessLogic.class.getName()).log(Level.SEVERE, null, ex);
        }
        return clerkVec;
    }

    public String addClerk(String fname, String lname, String pwd) {
        String str = null;
        try {
            str = Integer.toString(dao.addClerk(fname, lname, pwd));///need password encoding
        } catch (KwickKioskDBException ex) {
            JOptionPane.showMessageDialog(null, "Sorry! Can not connect to the server",
                    "Error", JOptionPane.ERROR_MESSAGE);
            Logger.getLogger(BusinessLogic.class.getName()).log(Level.SEVERE, null, ex);
        }
        return str;
    }

    public boolean deleteClerk(String clerkID) {
        bool = false;
        try {
            bool = dao.deleteClerk(Integer.parseInt(clerkID));
        } catch (KwickKioskDBException ex) {
            JOptionPane.showMessageDialog(null, "Sorry! Can not connect to the server",
                    "Error", JOptionPane.ERROR_MESSAGE);
            Logger.getLogger(BusinessLogic.class.getName()).log(Level.SEVERE, null, ex);
        }
        return bool;
    }

    public boolean updateClerk(String ckid, String password, String firstName,
            String lastName) {
        bool = false;
        try {
            bool = dao.updateClerk(new Clerk(Integer.parseInt(ckid), password, firstName, lastName));
        } catch (KwickKioskDBException ex) {
            JOptionPane.showMessageDialog(null, "Sorry! Can not connect to the server",
                    "Error", JOptionPane.ERROR_MESSAGE);
            Logger.getLogger(BusinessLogic.class.getName()).log(Level.SEVERE, null, ex);
        }
        return bool;

    }

    public Vector getClerkInfo(int ckid) {
        Vector vec = new Vector();
        Clerk cl = new Clerk();
        try {
            cl = dao.getClerkInfo(ckid);
        } catch (KwickKioskDBException ex) {
            JOptionPane.showMessageDialog(null, "Sorry! Can not connect to the server",
                    "Error", JOptionPane.ERROR_MESSAGE);
            Logger.getLogger(BusinessLogic.class.getName()).log(Level.SEVERE, null, ex);
        }
        if (cl == null) {
            return null;
        }
        vec.addElement(cl.getID());
        vec.addElement(cl.getFirstName());
        vec.addElement(cl.getLastName());
        vec.addElement(cl.getPassCode());
        clerkVec.clear();
        clerkVec.addElement(vec);
        return clerkVec;
    }

    public boolean addStore(String storeName, String phone, String taxCode,
            String aptNo, int stNo, String streetName,
            String postCode, String city, String province) {
        try {
            bool = dao.openNewStore(storeName, phone, taxCode, aptNo, stNo, streetName, postCode, city, province);
        } catch (KwickKioskDBException ex) {
            JOptionPane.showMessageDialog(null, "Sorry! Can not connect to the server",
                    "Error", JOptionPane.ERROR_MESSAGE);
            Logger.getLogger(BusinessLogic.class.getName()).log(Level.SEVERE, null, ex);
        }
        return bool;
    }

    public Vector getStore() {
        storeVec.clear();
        try {
            storeVec = transformToStoreVector(dao.getAllStores());
        } catch (KwickKioskDBException ex) {
            JOptionPane.showMessageDialog(null, "Sorry! Can not connect to the server",
                    "Error", JOptionPane.ERROR_MESSAGE);
            Logger.getLogger(BusinessLogic.class.getName()).log(Level.SEVERE, null, ex);
        }
        return storeVec;
    }

    public Vector transformToVector(ArrayList<Clerk> list) {
        Vector vect = new Vector();
        for (int i = 0; i < list.size(); i++) {
            Vector rec_vector = new Vector();
            rec_vector.addElement(list.get(i).getID());
            rec_vector.addElement(list.get(i).getFirstName());
            rec_vector.addElement(list.get(i).getLastName());
            rec_vector.addElement(list.get(i).getPassCode());
            vect.add(rec_vector);
        }
        return vect;
    }

    public Vector transformToStoreVector(ArrayList<Store> list) {
        Vector vect = new Vector();
        for (int i = 0; i < list.size(); i++) {
            Vector rec_vector = new Vector();
            rec_vector.addElement(list.get(i).getStoreName());
            rec_vector.addElement(list.get(i).getPhone());
            rec_vector.addElement(list.get(i).getAddress().getStNo());
            rec_vector.addElement(list.get(i).getAddress().getStreet());
            rec_vector.addElement(list.get(i).getAddress().getPostCode());
            rec_vector.addElement(list.get(i).getAddress().getCity());
            rec_vector.addElement(list.get(i).getAddress().getProvince());
            vect.add(rec_vector);
        }
        return vect;
    }

    public Vector transformToWholestoreVector(ArrayList<Store> list) {
        //       ArrayList list = dao.getStoresByStoreName(storeName);
        Vector vect = new Vector();
        for (int i = 0; i < list.size(); i++) {
            Vector rec_vector = new Vector();
            rec_vector.addElement(list.get(i).getStoreName());
            rec_vector.addElement(list.get(i).getAddress().getStNo());
            rec_vector.addElement(list.get(i).getAddress().getStreet());
            rec_vector.addElement(list.get(i).getAddress().getCity());
            rec_vector.addElement(list.get(i).getAddress().getPostCode());
            rec_vector.addElement(list.get(i).getAddress().getProvince());
            rec_vector.addElement(list.get(i).getPhone());
            rec_vector.addElement(list.get(i).getTaxCode());
            rec_vector.addElement(list.get(i).getAddress().getID());
            vect.add(rec_vector);
        }
        return vect;
    }

    public boolean updateStore(Store store) {
        String storeName = store.getStoreName();
        try {
            bool = dao.closeDownStore(storeName);
        } catch (KwickKioskDBException ex) {
            JOptionPane.showMessageDialog(null, "Sorry! Can not connect to the server",
                    "Error", JOptionPane.ERROR_MESSAGE);
            Logger.getLogger(BusinessLogic.class.getName()).log(Level.SEVERE, null, ex);

        }
        return bool;
    }

    public boolean deleteStore(String storeName) {
        try {
            //      return false;
            bool = dao.closeDownStore(storeName);
        } catch (KwickKioskDBException ex) {
            JOptionPane.showMessageDialog(null, "Sorry! Can not connect to the server",
                    "Error", JOptionPane.ERROR_MESSAGE);
            Logger.getLogger(BusinessLogic.class.getName()).log(Level.SEVERE, null, ex);
        }
        return bool;
    }

    public Vector getStoresByStoreName(String storename) {
        storeVec.clear();
        try {
            storeVec = transformToStoreVector(dao.searchByName(storename));
        } catch (KwickKioskDBException ex) {
            JOptionPane.showMessageDialog(null, "Sorry! Can not connect to the server",
                    "Error", JOptionPane.ERROR_MESSAGE);
            Logger.getLogger(BusinessLogic.class.getName()).log(Level.SEVERE,
                    null, ex);
        }
        return storeVec;
    }

    public Vector getWholeStoresByStoreName(String storename) {
        storeVec.clear();
        try {
            storeVec = transformToWholestoreVector(dao.searchByName(storename));
        } catch (KwickKioskDBException ex) {
            JOptionPane.showMessageDialog(null, "Sorry! Can not connect to the server",
                    "Error", JOptionPane.ERROR_MESSAGE);
            Logger.getLogger(BusinessLogic.class.getName()).log(Level.SEVERE,
                    null, ex);
        }
        return storeVec;
    }

    public Vector getStoresByPhonenumber(String phonenumber) {
        storeVec.clear();
        try {
            //          System.out.println("return value of search "+dao.searchByPhone(phonenumber));
            storeVec = transformToStoreVector(dao.searchByPhone(phonenumber));
        } catch (KwickKioskDBException ex) {
            JOptionPane.showMessageDialog(null, "Sorry! Can not connect to the server",
                    "Error", JOptionPane.ERROR_MESSAGE);
            Logger.getLogger(BusinessLogic.class.getName()).log(Level.SEVERE,
                    null, ex);
        }
        return storeVec;
    }

    public Vector getStoresByCity(String city) {
        storeVec.clear();
        try {
            storeVec = transformToStoreVector(dao.searchByCity(city));
        } catch (KwickKioskDBException ex) {
            JOptionPane.showMessageDialog(null, "Sorry! Can not connect to the server",
                    "Error", JOptionPane.ERROR_MESSAGE);
            Logger.getLogger(BusinessLogic.class.getName()).log(Level.SEVERE,
                    null, ex);
        }
        return storeVec;
    }

    public Vector getStoresByProvince(String province) {
        storeVec.clear();
        try {
            storeVec = transformToStoreVector(dao.searchByProvince(province));
        } catch (KwickKioskDBException ex) {
            JOptionPane.showMessageDialog(null, "Sorry! Can not connect to the server",
                    "Error", JOptionPane.ERROR_MESSAGE);
            Logger.getLogger(BusinessLogic.class.getName()).log(Level.SEVERE,
                    null, ex);
        }
        return storeVec;
    }

    public Vector getStoresByPostcode(String postcode) {
        storeVec.clear();
        try {
            storeVec = transformToStoreVector(dao.searchByPostCode(postcode));
        } catch (KwickKioskDBException ex) {
            JOptionPane.showMessageDialog(null, "Sorry! Can not connect to the server",
                    "Error", JOptionPane.ERROR_MESSAGE);
            Logger.getLogger(BusinessLogic.class.getName()).log(Level.SEVERE,
                    null, ex);
        }
        return storeVec;
    }

    public boolean addPromo(String PName, String sDate, String eDate,
            double PAmountD, int storeCard) {
        boolean b = false;
        try {
            //      System.out.println("in business");
            b = dao.openPromotion(PName, sDate, eDate, PAmountD, storeCard);
            //        System.out.println("in business and b is " + b);
        } catch (KwickKioskDBException ex) {
            JOptionPane.showMessageDialog(null, "Sorry! Can not connect to the server",
                    "Error", JOptionPane.ERROR_MESSAGE);
            Logger.getLogger(BusinessLogic.class.getName()).log(Level.SEVERE, null, ex);
        }
        return b;
    }

    public Vector getPromo() {
        promoVec.clear();
        //  System.out.print("inside the getpro of busi outside");
        try {
            //     System.out.print("inside the getpro of busi");
            promoVec = transformToPromoVector(dao.getAllPromotions());
        } catch (KwickKioskDBException ex) {
            JOptionPane.showMessageDialog(null, "Sorry! Can not connect to the server",
                    "Error", JOptionPane.ERROR_MESSAGE);
            Logger.getLogger(BusinessLogic.class.getName()).log(Level.SEVERE, null, ex);
        }
        return promoVec;
    }

    private Vector transformToPromoVector(ArrayList<Promotion> allPromotions) {
        Vector vect = new Vector();
        for (int i = 0; i < allPromotions.size(); i++) {
            Vector rec_vector = new Vector();
            rec_vector.addElement(allPromotions.get(i).getID());
            rec_vector.addElement(allPromotions.get(i).getName());
            rec_vector.addElement(allPromotions.get(i).getStartDate());
            rec_vector.addElement(allPromotions.get(i).getEndDate());
            rec_vector.addElement(allPromotions.get(i).getAmount());
            rec_vector.addElement(allPromotions.get(i).isStoreCardReq());   //the order maybe changed later
            vect.add(rec_vector);
        }
        return vect;
    }

    private Vector transformToSinglePromoVector(Promotion promotion) {
        Vector vect = new Vector();
        //   System.out.println("promotion's ID is: " + promotion.getID());
        for (int i = 0; i < 1; i++) {
            Vector rec_vector = new Vector();
            rec_vector.addElement(promotion.getID());
            rec_vector.addElement(promotion.getName());
            rec_vector.addElement(promotion.getStartDate());
            rec_vector.addElement(promotion.getEndDate());
            rec_vector.addElement(promotion.getAmount());
            rec_vector.addElement(promotion.isStoreCardReq());   //the order maybe changed later
            vect.add(rec_vector);
        }
        return vect;
    }

    public boolean deletePromo(String promoID) {
        bool = false;
        int ID = Integer.parseInt(promoID);
        try {
            bool = dao.removePromotion(ID);
        } catch (KwickKioskDBException ex) {
            JOptionPane.showMessageDialog(null, "Sorry! Can not connect to the server",
                    "Error", JOptionPane.ERROR_MESSAGE);
            Logger.getLogger(BusinessLogic.class.getName()).log(Level.SEVERE, null, ex);
        }
        return bool;
    }

    public Vector getPromosByID(String ID) {
        //  System.out.println("Inside getPromosByID and ID is: " + ID);
        promoVec.clear();
        int id = Integer.parseInt(ID);
        try {
            promoVec = transformToSinglePromoVector(dao.searchByID(id));
        } catch (KwickKioskDBException ex) {

            JOptionPane.showMessageDialog(null, "Sorry! Can not connect to the server",
                    "Error", JOptionPane.ERROR_MESSAGE);
            Logger.getLogger(BusinessLogic.class.getName()).log(Level.SEVERE, null, ex);
        }
        if (promoVec == null) {
            //     System.out.println("in busi and its null!");
            promoVec = new Vector();
        } else {
            //     System.out.println("promoVec is: " + promoVec);
        }
        return promoVec;

    }

    public Vector getPromosByStartdate(String Startdate) {
        promoVec.clear();
        try {
            promoVec = transformToPromoVector(dao.searchByStartDate(Startdate));
        } catch (KwickKioskDBException ex) {

            JOptionPane.showMessageDialog(null, "Sorry! Can not connect to the server",
                    "Error", JOptionPane.ERROR_MESSAGE);
            Logger.getLogger(BusinessLogic.class.getName()).log(Level.SEVERE, null, ex);
        }
        return promoVec;
    }

    public Vector getPromosByEnddate(String Enddate) {
        promoVec.clear();
        try {
            promoVec = transformToPromoVector(dao.searchByEndDate(Enddate));
        } catch (KwickKioskDBException ex) {
            JOptionPane.showMessageDialog(null, "Sorry! Can not connect to the server",
                    "Error", JOptionPane.ERROR_MESSAGE);
            Logger.getLogger(BusinessLogic.class.getName()).log(Level.SEVERE, null, ex);
        }
        return promoVec;
    }

    public Vector getItemByPromo(String prid) {
        itemVec.clear();
        int pid = Integer.parseInt(prid);
        try {
            itemVec = transformToItemVector(dao.getItemByPromo(pid));
            //    System.out.println("itemvec" + itemVec);
        } catch (KwickKioskDBException ex) {
            JOptionPane.showMessageDialog(null, "Sorry! Can not connect to the server",
                    "Error", JOptionPane.ERROR_MESSAGE);
            Logger.getLogger(BusinessLogic.class.getName()).log(Level.SEVERE, null, ex);
        }
        return itemVec;
    }

    public boolean removeItemFromPromotion(String upc, int prid) {
        try {
            bool = dao.removeItemFromPromotion(upc, prid);
        } catch (KwickKioskDBException ex) {
            JOptionPane.showMessageDialog(null, "Sorry! Can not connect to the server",
                    "Error", JOptionPane.ERROR_MESSAGE);
            Logger.getLogger(BusinessLogic.class.getName()).log(Level.SEVERE, null, ex);
        }
        return bool;
    }

    public int addItemToPromotion(String upc, int prid) {
        int checkValue = -1;
        bool = false;
        try {
            bool = dao.isItemInPromotion(upc, prid);
        } catch (KwickKioskDBException ex) {
            JOptionPane.showMessageDialog(null, "Sorry! Can not connect to the server",
                    "Error", JOptionPane.ERROR_MESSAGE);
            Logger.getLogger(BusinessLogic.class.getName()).log(Level.SEVERE, null, ex);
        }
        if (bool) {
            checkValue = 0;
            return checkValue;
        } else {
            try {
                dao.addItemToPromotion(upc, prid);
            } catch (KwickKioskDBException ex) {
                JOptionPane.showMessageDialog(null, "Sorry! Can not connect to the server",
                        "Error", JOptionPane.ERROR_MESSAGE);
                Logger.getLogger(BusinessLogic.class.getName()).log(Level.SEVERE, null, ex);
            }
            return checkValue;
        }
    }

    public boolean updatePromo(Promotion promo) {
        try {
            bool = dao.updatePromotion(promo);
        } catch (KwickKioskDBException ex) {
            JOptionPane.showMessageDialog(null, "Sorry! Can not connect to the server",
                    "Error", JOptionPane.ERROR_MESSAGE);
            Logger.getLogger(BusinessLogic.class.getName()).log(Level.SEVERE, null, ex);
        }
        return bool;
    }

    public Vector getItem() {
        itemVec.clear();
        try {
            itemVec = transformToItemVector(dao.getAllItems());
            //     System.out.println("itemVec is: " + itemVec);
        } catch (KwickKioskDBException ex) {
            JOptionPane.showMessageDialog(null, "Sorry! Can not connect to the server",
                    "Error", JOptionPane.ERROR_MESSAGE);
            Logger.getLogger(BusinessLogic.class.getName()).log(Level.SEVERE, null, ex);
        }
        return itemVec;
    }

    private Vector transformToItemVector(ArrayList<Item> allItems) {
        Vector vect = new Vector();
        for (int i = 0; i < allItems.size(); i++) {
            Vector rec_vector = new Vector();
            rec_vector.addElement(allItems.get(i).getUpc());
            rec_vector.addElement(allItems.get(i).getName());
            rec_vector.addElement(allItems.get(i).getMake());
            rec_vector.addElement(allItems.get(i).getModel());
            rec_vector.addElement(allItems.get(i).getCostPrice());
            rec_vector.addElement(allItems.get(i).getRetailPrice());
            rec_vector.addElement(allItems.get(i).getDescription());
            rec_vector.addElement(allItems.get(i).getImgURL());
            rec_vector.addElement(allItems.get(i).getCategoryID());
            vect.add(rec_vector);
        }
        return vect;
    }

    public boolean addItem(Item i) {
        bool = false;
        try {
            if (dao.searchItemByUPC(i.getUpc()) != null) {
                return false;
            } else {
                try {
                    bool = dao.addItemToInventory(i);
                } catch (KwickKioskDBException ex) {
                    JOptionPane.showMessageDialog(null, "Sorry! Can not connect to the server",
                            "Error", JOptionPane.ERROR_MESSAGE);
                    Logger.getLogger(BusinessLogic.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        } catch (KwickKioskDBException ex) {
            JOptionPane.showMessageDialog(null, "Sorry! Can not connect to the server",
                    "Error", JOptionPane.ERROR_MESSAGE);
            Logger.getLogger(BusinessLogic.class.getName()).log(Level.SEVERE, null, ex);
        }
        return bool;
    }

    public Vector getItemsByName(String name) {
        itemVec.clear();
        try {
            itemVec = transformToItemVector(dao.searchItemByName(name));
            //   System.out.println("in logic and itemVec is: " + itemVec);
        } catch (KwickKioskDBException ex) {
            JOptionPane.showMessageDialog(null, "Sorry! Can not connect to the server",
                    "Error", JOptionPane.ERROR_MESSAGE);
            Logger.getLogger(BusinessLogic.class.getName()).log(Level.SEVERE, null, ex);
        }
        return itemVec;
    }

    public Vector getItemByUPC(String UPC) {
        itemVec.clear();
        try {
            itemVec = transformToSingleItemVector(dao.searchItemByUPC(UPC));
            //  System.out.println("in logic and itemVec is: " + itemVec);
        } catch (KwickKioskDBException ex) {
            JOptionPane.showMessageDialog(null, "Sorry! Can not connect to the server",
                    "Error", JOptionPane.ERROR_MESSAGE);
            Logger.getLogger(BusinessLogic.class.getName()).log(Level.SEVERE, null, ex);
        }
        if (itemVec == null) {
            itemVec = new Vector();
        }
        return itemVec;
    }

    private Vector transformToSingleItemVector(Item item) {
        Vector vect = new Vector();
        if (item == null) {
            return null;
        }
        for (int i = 0; i < 1; i++) {
            Vector rec_vector = new Vector();
            rec_vector.addElement(item.getUpc());
            rec_vector.addElement(item.getName());
            rec_vector.addElement(item.getMake());
            rec_vector.addElement(item.getModel());
            rec_vector.addElement(item.getCostPrice());
            rec_vector.addElement(item.getRetailPrice());
            rec_vector.addElement(item.getDescription());
            rec_vector.addElement(item.getImgURL());
            rec_vector.addElement(item.getCategoryID());
            vect.add(rec_vector);
        }
        return vect;
    }

    public Vector getItemsByMake(String make) {
        itemVec.clear();
        try {
            itemVec = transformToItemVector(dao.searchItemByMake(make));
            //   System.out.println("in logic and itemVec is: " + itemVec);
        } catch (KwickKioskDBException ex) {
            JOptionPane.showMessageDialog(null, "Sorry! Can not connect to the server",
                    "Error", JOptionPane.ERROR_MESSAGE);
            Logger.getLogger(BusinessLogic.class.getName()).log(Level.SEVERE, null, ex);
        }
        return itemVec;
    }

    public Vector getItemsByModel(String model) {
        itemVec.clear();
        try {
            itemVec = transformToItemVector(dao.searchItemByModel(model));
            //  System.out.println("in logic and itemVec is: " + itemVec);
        } catch (KwickKioskDBException ex) {
            JOptionPane.showMessageDialog(null, "Sorry! Can not connect to the server",
                    "Error", JOptionPane.ERROR_MESSAGE);
            Logger.getLogger(BusinessLogic.class.getName()).log(Level.SEVERE, null, ex);
        }
        return itemVec;
    }

    public boolean deleteItem(String UPC) {
        bool = false;
        try {
            bool = dao.takeItemOffTheShelf(UPC);
        } catch (KwickKioskDBException ex) {
            JOptionPane.showMessageDialog(null, "Sorry! Can not connect to the server",
                    "Error", JOptionPane.ERROR_MESSAGE);
            Logger.getLogger(BusinessLogic.class.getName()).log(Level.SEVERE, null, ex);
        }
        return bool;
    }

    public Vector getOrder() {
        orderVec.clear();
        try {
            itemVec = transformToItemVector(dao.getAllItems());
            //    System.out.println("itemVec is: " + itemVec);
        } catch (KwickKioskDBException ex) {
            JOptionPane.showMessageDialog(null, "Sorry! Can not connect to the server",
                    "Error", JOptionPane.ERROR_MESSAGE);
            Logger.getLogger(BusinessLogic.class.getName()).log(Level.SEVERE, null, ex);
        }
        return itemVec;
    }

    public Vector getAllOrder() {
        orderVec.clear();
        try {
            orderVec = transformToOrderVector(dao.getAllOrder());
        } catch (KwickKioskDBException ex) {
            JOptionPane.showMessageDialog(null, "Sorry! Can not connect to the server",
                    "Error", JOptionPane.ERROR_MESSAGE);
            Logger.getLogger(BusinessLogic.class.getName()).log(Level.SEVERE, null, ex);

        }
        return orderVec;
    }

    public Vector getOrderByID(String ID) {
        orderVec.clear();
        int id = Integer.parseInt(ID);
        try {
            orderVec = transformToSingleOrderVector(dao.searchByOrderNum(id));
            //    System.out.println("in logic and itemVec is: " + orderVec);
        } catch (KwickKioskDBException ex) {
            JOptionPane.showMessageDialog(null, "Sorry! Can not connect to the server",
                    "Error", JOptionPane.ERROR_MESSAGE);
            Logger.getLogger(BusinessLogic.class.getName()).log(Level.SEVERE, null, ex);
        }
        if (orderVec == null) {
            orderVec = new Vector();
        }
        return orderVec;
    }

    private Vector transformToSingleOrderVector(Order order) {
        Vector vect = new Vector();
        if (order == null) {
            return null;
        }
        for (int i = 0; i < 1; i++) {
            Vector rec_vector = new Vector();
            rec_vector.addElement(order.getID());
            rec_vector.addElement(order.getOrderDate());
            rec_vector.addElement(order.getOrderStatus());
            rec_vector.addElement(order.getItemSubTotal());
            rec_vector.addElement(order.getTaxAmount());
            rec_vector.addElement(order.getShippingFee());
            rec_vector.addElement(order.getOrderDiscount());
            vect.add(rec_vector);
        }
        return vect;
    }

    public Vector getOrderByDate(String Date) {
        orderVec.clear();
        try {
            orderVec = transformToOrderVector(dao.searchByDate(Date));
            //   System.out.println("in logic and itemVec is: " + orderVec);
        } catch (KwickKioskDBException ex) {
            JOptionPane.showMessageDialog(null, "Sorry! Can not connect to the server",
                    "Error", JOptionPane.ERROR_MESSAGE);
            Logger.getLogger(BusinessLogic.class.getName()).log(Level.SEVERE, null, ex);
        }
        return orderVec;
    }

    private Vector transformToOrderVector(ArrayList<Order> allOrders) {
        Vector vect = new Vector();
        for (int i = 0; i < allOrders.size(); i++) {
            Vector rec_vector = new Vector();
            rec_vector.addElement(allOrders.get(i).getID());
            rec_vector.addElement(allOrders.get(i).getOrderDate());
            rec_vector.addElement(allOrders.get(i).getOrderStatus());
            rec_vector.addElement(allOrders.get(i).getItemSubTotal());
            rec_vector.addElement(allOrders.get(i).getTaxAmount());
            rec_vector.addElement(allOrders.get(i).getShippingFee());
            rec_vector.addElement(allOrders.get(i).getOrderDiscount());
            vect.add(rec_vector);
        }
        return vect;
    }

    public Vector getOrderByStatus(String status) {
        orderVec.clear();
        try {
            orderVec = transformToOrderVector(dao.searchByStatus(status));
            //    System.out.println("in logic and itemVec is: " + orderVec);
        } catch (KwickKioskDBException ex) {
            JOptionPane.showMessageDialog(null, "Sorry! Can not connect to the server",
                    "Error", JOptionPane.ERROR_MESSAGE);
            Logger.getLogger(BusinessLogic.class.getName()).log(Level.SEVERE, null, ex);
        }
        return orderVec;
    }

    public boolean updateOrderstatus(int id, String status) {
        bool = false;
        try {
            bool = dao.updateOrderStatus(id, status);
        } catch (KwickKioskDBException ex) {
            JOptionPane.showMessageDialog(null, "Sorry! Can not connect to the server",
                    "Error", JOptionPane.ERROR_MESSAGE);
            Logger.getLogger(BusinessLogic.class.getName()).log(Level.SEVERE, null, ex);
        }
        return bool;
    }

    public Vector getStoresReport() {
        storeVec.clear();
        try {
            storeVec = transformToStoreReportVector(dao.getAllStores());
        } catch (KwickKioskDBException ex) {
            JOptionPane.showMessageDialog(null, "Sorry! Can not connect to the server",
                    "Error", JOptionPane.ERROR_MESSAGE);
            Logger.getLogger(BusinessLogic.class.getName()).log(Level.SEVERE, null, ex);
        }
        return storeVec;
    }

    private Vector transformToStoreReportVector(ArrayList<Store> allStores) {
        Vector vect = new Vector();
        for (int i = 0; i < allStores.size(); i++) {
            Vector rec_vector = new Vector();
            rec_vector.addElement(allStores.get(i).getStoreName());
            rec_vector.addElement(allStores.get(i).getAddress().getPostCode());
            rec_vector.addElement(allStores.get(i).getAddress().getCity());
            rec_vector.addElement(allStores.get(i).getAddress().getProvince());
            vect.add(rec_vector);
        }
        return vect;
    }

    public Vector getStoresReportByStoreName(String name) {
        storeVec.clear();
        //  System.out.println("in Logic and name is: " + name);
        try {
            //     System.out.println("return value of search "+dao.searchByName(storename));
            storeVec = transformToStoreReportVector(dao.searchByName(name));
        } catch (KwickKioskDBException ex) {
            JOptionPane.showMessageDialog(null, "Sorry! Can not connect to the server",
                    "Error", JOptionPane.ERROR_MESSAGE);
            Logger.getLogger(BusinessLogic.class.getName()).log(Level.SEVERE,
                    null, ex);
        }
        return storeVec;
    }

    public Vector getStoresReportByStorePosCode(String posCode) {
        storeVec.clear();
        //    System.out.println("in Logic and name is: " + posCode);
        try {
            storeVec = transformToStoreReportVector(
                    dao.searchByPostCode(posCode));
        } catch (KwickKioskDBException ex) {
            JOptionPane.showMessageDialog(null, "Sorry! Can not connect to the server",
                    "Error", JOptionPane.ERROR_MESSAGE);
            Logger.getLogger(BusinessLogic.class.getName()).log(Level.SEVERE,
                    null, ex);
        }
        return storeVec;
    }

    public Vector getStoresReportByStoreCity(String city) {
        storeVec.clear();
        //   System.out.println("in Logic and name is: " + city);
        try {
            storeVec = transformToStoreReportVector(dao.searchByPostCode(city));
        } catch (KwickKioskDBException ex) {
            JOptionPane.showMessageDialog(null, "Sorry! Can not connect to the server",
                    "Error", JOptionPane.ERROR_MESSAGE);
            Logger.getLogger(BusinessLogic.class.getName()).log(Level.SEVERE,
                    null, ex);
        }
        return storeVec;
    }

    public Vector getStoresReportByStoreProvince(String province) {
        storeVec.clear();
        //  System.out.println("in Logic and name is: " + province);
        try {
            storeVec = transformToStoreReportVector(dao.searchByPostCode(
                    province));
        } catch (KwickKioskDBException ex) {
            JOptionPane.showMessageDialog(null, "Sorry! Can not connect to the server",
                    "Error", JOptionPane.ERROR_MESSAGE);
            Logger.getLogger(BusinessLogic.class.getName()).log(Level.SEVERE,
                    null, ex);
        }
        return storeVec;
    }

    public Vector getItemsReport() {
        itemVec.clear();
        try {
            itemVec = transformToItemVector(dao.getAllItems());
        } catch (KwickKioskDBException ex) {
            JOptionPane.showMessageDialog(null, "Sorry! Can not connect to the server",
                    "Error", JOptionPane.ERROR_MESSAGE);
            Logger.getLogger(BusinessLogic.class.getName()).log(Level.SEVERE, null, ex);
        }
        return itemVec;
    }

    public Vector<Vector> itemReport(List<String> upcList,
            String startDate, String endDate) {
        Vector reportVector = null;
        //   System.out.println("startDate in busi " + startDate + endDate);
        try {
            reportVector = dao.getItemReport(upcList, startDate, endDate);
        } catch (KwickKioskDBException ex) {
            JOptionPane.showMessageDialog(null, "Sorry! Can not connect to the server",
                    "Error", JOptionPane.ERROR_MESSAGE);
            Logger.getLogger(BusinessLogic.class.getName()).log(Level.SEVERE, null, ex);
        }
        return reportVector;
    }

    public Vector<Vector> storeReport(List<String> storeList,
            String startDate, String endDate) {
        Vector reportVector = null;
        try {
            reportVector = dao.getStoreReport(storeList, startDate, endDate);
        } catch (KwickKioskDBException ex) {
            JOptionPane.showMessageDialog(null, "Sorry! Can not connect to the server",
                    "Error", JOptionPane.ERROR_MESSAGE);
            Logger.getLogger(BusinessLogic.class.getName()).log(Level.SEVERE, null, ex);
        }
        return reportVector;
    }

    public Vector<Vector> getInventoryReport(String storeName) {
        Vector reportVector = null;
        try {
            reportVector = dao.getInventoryReport(storeName);
        } catch (KwickKioskDBException ex) {
            JOptionPane.showMessageDialog(null, "Sorry! Can not connect to the server",
                    "Error", JOptionPane.ERROR_MESSAGE);
            Logger.getLogger(BusinessLogic.class.getName()).log(Level.SEVERE, null, ex);

        }
        return reportVector;
    }

    public Vector<Vector> getAllPostCode() {
        Vector<Vector> report = null;
        try {
            report = dao.getPostCode();
        } catch (KwickKioskDBException ex) {
            JOptionPane.showMessageDialog(null, "Sorry! Can not connect to the server",
                    "Error", JOptionPane.ERROR_MESSAGE);
            Logger.getLogger(BusinessLogic.class.getName()).log(Level.SEVERE, null, ex);
        }
        return report;
    }

    public Vector<Vector> getPostCodeByCity(String city) {
        Vector<Vector> report = null;
        try {
            report = dao.getPostCodeByCity(city);
        } catch (KwickKioskDBException ex) {
            JOptionPane.showMessageDialog(null, "Sorry! Can not connect to the server",
                    "Error", JOptionPane.ERROR_MESSAGE);
            Logger.getLogger(BusinessLogic.class.getName()).log(Level.SEVERE, null, ex);
        }
        return report;
    }

    public Vector<Vector> getPostCodeByProvince(String province) {
        Vector<Vector> report = null;
        try {
            report = dao.getPostCodeByProvince(province);
        } catch (KwickKioskDBException ex) {
            JOptionPane.showMessageDialog(null, "Sorry! Can not connect to the server",
                    "Error", JOptionPane.ERROR_MESSAGE);
            Logger.getLogger(BusinessLogic.class.getName()).log(Level.SEVERE, null, ex);
        }
        return report;
    }

    public Vector<Vector> getAreaReport(List<String> postCodeList,
            String startDate, String endDate) {
        Vector<Vector> report = new Vector<Vector>();
        try {
            report = dao.getAreaReport(postCodeList, startDate, endDate);
        } catch (KwickKioskDBException ex) {
            JOptionPane.showMessageDialog(null, "Sorry! Can not connect to the server",
                    "Error", JOptionPane.ERROR_MESSAGE);
            Logger.getLogger(BusinessLogic.class.getName()).log(Level.SEVERE, null, ex);

        }
        return report;
    }

    public boolean updateItemQTYInStore(String storeName, String upc, int qty) {
        try {
            bool = dao.updateItemQTYInStore(storeName, upc, qty);
        } catch (KwickKioskDBException ex) {
            JOptionPane.showMessageDialog(null, "Sorry! Can not connect to the server",
                    "Error", JOptionPane.ERROR_MESSAGE);
            Logger.getLogger(BusinessLogic.class.getName()).log(Level.SEVERE, null, ex);
        }
        return bool;
    }

    public int getItemQtyInStore(String storeName, String upc) {
        int qty = 0;
        try {
            qty = dao.getItemQtyInStore(storeName, upc);
        } catch (KwickKioskDBException ex) {
            JOptionPane.showMessageDialog(null, "Sorry! Can not connect to the server",
                    "Error", JOptionPane.ERROR_MESSAGE);
            Logger.getLogger(BusinessLogic.class.getName()).log(Level.SEVERE, null, ex);
        }
        return qty;
    }

    public boolean updateItemInfo(String oldUPC, Item i) {
        try {
            bool = dao.updateItemInfo(oldUPC, i);
        } catch (KwickKioskDBException ex) {
            JOptionPane.showMessageDialog(null, "Sorry! Can not connect to the server",
                    "Error", JOptionPane.ERROR_MESSAGE);
            Logger.getLogger(BusinessLogic.class.getName()).log(Level.SEVERE, null, ex);
        }
        return bool;
    }

    public boolean addItemToStore(String storeName, String upc, int i) {
        try {
            bool = dao.addItemToStore(storeName, upc, i);
        } catch (KwickKioskDBException ex) {
            JOptionPane.showMessageDialog(null, "Sorry! Can not connect to the server",
                    "Error", JOptionPane.ERROR_MESSAGE);
            Logger.getLogger(BusinessLogic.class.getName()).log(Level.SEVERE, null, ex);
        }
        return bool;
    }

    public ArrayList<Category> getCategories() {
        ArrayList<Category> listCategory = new ArrayList<Category>();
        try {
            listCategory = dao.getCategories();
        } catch (KwickKioskDBException ex) {
            JOptionPane.showMessageDialog(null, "Sorry! Can not connect to the server",
                    "Error", JOptionPane.ERROR_MESSAGE);
            Logger.getLogger(BusinessLogic.class.getName()).log(Level.SEVERE, null, ex);
        }
        return listCategory;
    }

    public Vector getAdminItemsByCategory(int ctid) {
        itemVec.clear();
        try {
            itemVec = transformToItemVector(dao.getAdminItemsByCategory(ctid));
        } catch (KwickKioskDBException ex) {
            JOptionPane.showMessageDialog(null, "Sorry! Can not connect to the server",
                    "Error", JOptionPane.ERROR_MESSAGE);
            Logger.getLogger(BusinessLogic.class.getName()).log(Level.SEVERE, null, ex);
        }
        return itemVec;
    }

    public boolean isItemInPromotion(String upc, int prid) {
        bool = false;
        try {
            bool = dao.isItemInPromotion(upc, prid);
        } catch (KwickKioskDBException ex) {
            JOptionPane.showMessageDialog(null, "Sorry! Can not connect to the server",
                    "Error", JOptionPane.ERROR_MESSAGE);
            Logger.getLogger(BusinessLogic.class.getName()).log(Level.SEVERE, null, ex);
        }
        return bool;
    }
}
